<html>
<head><meta charset="utf-8"><title>Get all value mutations clippy#4310 · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html">Get all value mutations clippy#4310</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226253121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226253121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226253121">(Feb 13 2021 at 16:23)</a>:</h4>
<p>I'm currently working on a <em>lint</em> which requires me to get all possible mutations of a value. The only way that I can think of is to check for values with the requested type and then use a <code>Visitor</code> that goes through the scope to collect all possible mutations. This is a <em>very</em> oversimplified example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">cool_code</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Create a visitor for this value and track it</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">applicability</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MachineApplicable</span>::<span class="n">MachineApplicable</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">some_condition</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">applicability</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MachineApplicable</span>::<span class="n">HasPlaceholders</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="n">span_lint_and_suggest</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="o">..</span><span class="p">.,</span><span class="w"></span>
<span class="w">        </span><span class="n">applicability</span><span class="p">,</span><span class="w"> </span><span class="c1">// The visitor reports all possible mutations: ["MachineApplicable", "HasPlaceholders"]</span>
<span class="w">        </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">    </span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This obviously also requires some logic to handle <code>&amp;mut</code> passes.</p>
<p>Is there a simpler way to do this? <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> <span aria-label="penguin" class="emoji emoji-1f427" role="img" title="penguin">:penguin:</span></p>



<a name="226263539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226263539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226263539">(Feb 13 2021 at 19:54)</a>:</h4>
<p>ExprUseVisitor?</p>



<a name="226263878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226263878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226263878">(Feb 13 2021 at 20:01)</a>:</h4>
<p>That looks exactly like what I was looking for! Just in time before I wanted to actually write a complete new visitor. THANK YOU!!!!!</p>



<a name="226266412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226266412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226266412">(Feb 13 2021 at 20:50)</a>:</h4>
<p>Something it took me a while to understand with ExprUseVisitor - it doesn't really tell you <em>how</em> the value is used or anything about the context of how it is used. That part is up to you to implement.</p>



<a name="226266446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226266446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226266446">(Feb 13 2021 at 20:51)</a>:</h4>
<p>But it does tell you if the value is moved, copied or mutated</p>



<a name="226266643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226266643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226266643">(Feb 13 2021 at 20:54)</a>:</h4>
<p>That kind of makes sense. Thanks for the heads up and now I know which expert I can ask ;)</p>



<a name="226734514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226734514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226734514">(Feb 17 2021 at 22:08)</a>:</h4>
<p>I've tried to use the <code>ExprUseVisitor</code> but I'm unable to get the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/def_id/struct.LocalDefId.html"><code>LocalDefId</code></a> of the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Local.html"><code>hir::Local</code></a> instance which is required by the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/expr_use_visitor/struct.ExprUseVisitor.html#method.new">constructor</a>. A quick dump of all <code>LocalDefId</code>'s in <code>cx.tcx.hir()</code> only shows IDs for Items. <span class="user-mention" data-user-id="360405">@Cameron Steffen</span> have you ever used the visitor for actual locals? All current uses within Clippy only reference functions <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="226735337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226735337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226735337">(Feb 17 2021 at 22:11)</a>:</h4>
<p>The LocalDefId is for the body that you are visiting inside, not the Local</p>



<a name="226735615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226735615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226735615">(Feb 17 2021 at 22:13)</a>:</h4>
<p>Where do I define the local ID that I'm interested in then?</p>



<a name="226735697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226735697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226735697">(Feb 17 2021 at 22:13)</a>:</h4>
<p>Or do do you mean that it just loops over the expressions in the block without any filtering? <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="226735759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226735759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226735759">(Feb 17 2021 at 22:13)</a>:</h4>
<p>Yes</p>



<a name="226736049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226736049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226736049">(Feb 17 2021 at 22:14)</a>:</h4>
<p>It's almost like a normal visitor, but with a little extra info in the callbacks</p>



<a name="226736264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226736264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226736264">(Feb 17 2021 at 22:15)</a>:</h4>
<p>Ahhh, okay, I've misunderstood the usage so far. Thank you for clearing that up <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="226740843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226740843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226740843">(Feb 17 2021 at 22:57)</a>:</h4>
<p>You're welcome!</p>



<a name="226741270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Get%20all%20value%20mutations%20clippy%234310/near/226741270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Get.20all.20value.20mutations.20clippy.234310.html#226741270">(Feb 17 2021 at 23:01)</a>:</h4>
<p>It works now! <span aria-label="blush" class="emoji emoji-1f60a" role="img" title="blush">:blush:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>